#include <stdio.h>
#include <stdlib.h>
#include "double_list.h"

//#if defined(_D_LIST_VER4_)
struct test{
    int elem;
    char name[10];
};

int my_cmp(void *da, void *db)
{
    struct test *node_a, *node_b;
    if (NULL == da || NULL == db)
    {
	printf("Invalid parameters\n");
	return -1;
    }

    node_a = (struct test *)da;
    node_b = (struct test *)db;

    if (node_a->elem == node_b->elem)
    {
	return 0;
    }

    return 1;
}

int print_node(void *node, void *ctx)
{
    struct test *temp = (struct test *)node;
    if (NULL == node)
    {
	printf("Invalid parameters\n");
	return -1;
    }
    printf("Elem:\t%d\n", temp->elem);
    printf("Name:\t%s\n", temp->name);

    return 0;
}

int destroy_node(void *data)
{
    if (NULL != data)
    {
	free((struct test *)data);
	data = NULL;
    }

    return 0;
}

DList list;
DList ist;
int main(int argc, char **argv)
{
    int i;
    int count;
    int data;
    DNode *node;
    struct test *new_data;
    init_list(&list, my_cmp, destroy_node);
    printf("Please input the elemet you want to manage:\n");
    scanf("%d", &count);
    for (i = 0; i < count; i++)
    {
	new_data = (struct test *)malloc(sizeof(struct test));
	printf("Please input the elem value:\n");
	scanf("%d", &(new_data->elem));
	printf("Please input the name:\n");
	scanf("%s", new_data->name);
	append_to_list(&list, (void *)new_data);
    }



    list_for_each(&list, print_node, NULL);
//    destroy_list(&list);

    printf("Please input the element you want to delete\n");
    struct test my_node;
    scanf("%d", &my_node.elem);
    remove_node(&list, (void *)(&my_node));

    list_for_each(&list, print_node, NULL);
    destroy_list(&list);
    return 0;
}

//#endif
